import {
	apiBaseUrl
} from './config.js';
import * as common from './common.js' //引入common
// 需要登陆的，都写到这里，否则就是不需要登陆的接口
const methodsToken = [
  'apistore/user/user_show_little'
];

const post = (method, data, callback,complete) => {
	uni.showLoading({
		title: "加载中",
		mask: true,
	});
	var isLogin = 0;
	var header = {
			'Accept': 'application/json',
			'Content-Type': 'application/json',
			// 'Content-Type': 'application/x-www-form-urlencoded', //自定义请求头信息
		}
	// 判断token是否存在
	if (methodsToken.indexOf(method) >= 0) {
		isLogin = 1;
		// 获取用户token
		
		try {
		    var token = uni.getStorageSync("token");
		    if (token) {
		        console.log(token);
				header = {
					'Accept': 'application/json',
					'Content-Type': 'application/json',
					'token':token,
					// 'Content-Type': 'application/x-www-form-urlencoded', //自定义请求头信息
				}
		    }
		} catch (e) {
		    console.log('未获取到token');
		}
	}
	uni.request({
		url: apiBaseUrl+method,
		data: data,
		header: header,
		method: 'POST',
		success: (response) => {
			uni.hideLoading();
			const result = response.data
			callback(result);
		},
		complete: (response) => {
			setTimeout(function() {
				uni.hideLoading();
			}, 1000)
			complete?complete(): "";
		},
		fail: (error) => {
			uni.showLoading({
				title: '网络开小差了'
			});
			setTimeout(function() {
				uni.hideLoading();
			}, 1000)
			if (error && error.response) {
				showError(error.response);
			} else {
				
			}
			
		},
	});

}
const get = (method, data, callback,complete) => {
	// uni.showLoading({
	// 	title: '加载中',
	// 	mask: true,
	// })
	var header = {
			'Accept': 'application/json',
			'Content-Type': 'application/json',
			// 'Content-Type': 'application/x-www-form-urlencoded', //自定义请求头信息
		}
	// 判断token是否存在
	if (methodsToken.indexOf(method) >= 0) {
		// 获取用户token
		try {
		    var token = uni.getStorageSync("token");
		    if (token) {
		        console.log(token);
				header = {
					'Accept': 'application/json',
					'Content-Type': 'application/json',
					'token':token,
					// 'Content-Type': 'application/x-www-form-urlencoded', //自定义请求头信息
				}
		    }
		} catch (e) {
		    console.log('未获取到token');
		}
	}
	uni.request({
		url: apiBaseUrl+method,
		data: data,
		header: header,
		method: 'GET',
		success: (response) => {
			//uni.hideLoading();
			callback(response.data);
		},
		fail: (error) => {
			//uni.hideLoading();
			if (error && error.response) {
				showError(error.response);
			}
		},
		complete: () => {
			setTimeout(function() {
				//uni.hideLoading();
			}, 250);
		}
	});
}
const showError = error => {
	let errorMsg = ''
	switch (error.status) {
		case 400:
			errorMsg = '请求参数错误'
			break
		case 401:
			errorMsg = '未授权，请登录'
			break
		case 403:
			errorMsg = '跨域拒绝访问'
			break
		case 404:
			errorMsg = `请求地址出错: ${error.config.url}`
			break
		case 408:
			errorMsg = '请求超时'
			break
		case 500:
			errorMsg = '服务器内部错误'
			break
		case 501:
			errorMsg = '服务未实现'
			break
		case 502:
			errorMsg = '网关错误'
			break
		case 503:
			errorMsg = '服务不可用'
			break
		case 504:
			errorMsg = '网关超时'
			break
		case 505:
			errorMsg = 'HTTP版本不受支持'
			break
		default:
			errorMsg = error.msg
			break
	}

	uni.showToast({
		title: errorMsg, 
		icon: 'none',
		duration: 1000,
		complete: function() {
			setTimeout(function() {
				uni.hideToast();
			}, 1000);
		}
	});  
}

export const get_company_dept = (data, callback) => post('/wx/user/get-company-dept', data, callback);
export const sign_in = (data, callback) => post('/wx/user/sign-in', data, callback);
